package com.cx.mall.demo.service.impl;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cx.mall.demo.mapper.DemoUserLoginLogMapper;
import com.cx.mall.demo.model.UserLoginLog;
import com.cx.mall.demo.model.UserLoginLogDTO;
import com.cx.mall.demo.service.IDemoUserLoginLogService;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service("demoUserLoginLogServiceImpl")
public class DemoUserLoginLogServiceImpl extends ServiceImpl<DemoUserLoginLogMapper, UserLoginLog> implements IDemoUserLoginLogService {

    @Override
    public List<UserLoginLog> queryUserLoginLogByUserId(long userId) {
        LocalDateTime startDate = LocalDate.of(2025, 7, 4).atStartOfDay();
        // 1.构建条件构造器 :构建一个到user_login_log表的查询条件构造器
        LambdaQueryChainWrapper<UserLoginLog> wrapper = new LambdaQueryChainWrapper<>(this.getBaseMapper());
        //2.创建查询条件
        List<UserLoginLog> list = wrapper.eq(UserLoginLog::getUserId, userId)
                //在2025年7月4日之后登陆的
                .ge(UserLoginLog::getCreateTime,startDate)
                .orderByDesc(UserLoginLog::getCreateTime)
                .list();

        return list;
    }

    @Override
    public List<UserLoginLog> queryUserLoginLog(UserLoginLogDTO query) {
        //1.创建条件构造器
        LambdaQueryChainWrapper<UserLoginLog> wrapper = new LambdaQueryChainWrapper<>(baseMapper);
        //2.填充查询条件
        //2.1按用户ID查询
        if(query.getUserId() != null){
            wrapper.eq(UserLoginLog::getUserId,query.getUserId());
        }
        //2.2按登陆时间查询
        if(query.getStartTime() != null && query.getEndTime() != null){
            wrapper.between(UserLoginLog::getCreateTime,query.getStartTime(),query.getEndTime());
        }
        //2.3按登陆IP查询
        if(query.getIp() != null){
            wrapper.eq(UserLoginLog::getIp,query.getIp());
        }

        //3.提出数据
        List<UserLoginLog> list = wrapper.list();
        return list;
    }
}
